package com.nineclock.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.NcConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.system.dto.SysCompanyContactConfigDTO;
import com.nineclock.system.mapper.SysCompanyContactConfigMapper;
import com.nineclock.system.pojo.SysCompanyContactConfig;
import com.nineclock.system.service.SysCompanyContactConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SysCompanyContactConfigServiceImpl implements SysCompanyContactConfigService {
    @Autowired
    private SysCompanyContactConfigMapper companyContactConfigMapper;
    @Override
    public void addCompanyContactConfig(SysCompanyContactConfigDTO configDTO) {
        long companyId= CurrentUserHolder.get().getCompanyId();
        //健壮性判断
        if (configDTO == null || StrUtil.isEmpty(configDTO.getName())){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);

        }

        //添加数据
        SysCompanyContactConfig contactConfig = BeanHelper.copyProperties(configDTO, SysCompanyContactConfig.class);
        contactConfig.setStatus((short)1);
        contactConfig.setCompanyId(companyId);
        contactConfig.setFieldType("string");
        companyContactConfigMapper.insert(contactConfig);


    }

    @Override
    public void updataStatus(Long id, Short status) {
        long companyId= CurrentUserHolder.get().getCompanyId();
        SysCompanyContactConfig contactConfig = companyContactConfigMapper.selectById(id);

        if (contactConfig==null ){
            throw new NcException(ResponseEnum.DATA_NOT_EXISTS);
        }
        contactConfig.setCompanyId(companyId);
        contactConfig.setStatus(status);
        companyContactConfigMapper.updateById(contactConfig);

    }

    @Override
    public List<SysCompanyContactConfigDTO> querrCompnayContactConfig() {
        List<SysCompanyContactConfigDTO> dtoList = new ArrayList<>();
        long companyId = CurrentUserHolder.get().getCompanyId();
        //构造条件查询
        LambdaQueryWrapper<SysCompanyContactConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysCompanyContactConfig::getCompanyId, companyId);
        List<SysCompanyContactConfig> sysCompanyContactConfigs = companyContactConfigMapper.selectList(queryWrapper);

        //如果企业没有设置通讯录字段, 需要初始化默认值
        if(CollectionUtil.isEmpty(sysCompanyContactConfigs)) {
            for (String fieldName: NcConstant.COMPANY_CONFIG_CONTACT_LIST){
                SysCompanyContactConfig config = new SysCompanyContactConfig();
                config.setStatus((short)1);
                config.setType("fixed");
                config.setName(fieldName);
                config.setFieldType("String");
                config.setCompanyId(companyId);
                companyContactConfigMapper.insert(config);

                dtoList.add(BeanHelper.copyProperties(config,SysCompanyContactConfigDTO.class));
            }
            return dtoList;
        }
        return BeanHelper.copyWithCollection(sysCompanyContactConfigs,SysCompanyContactConfigDTO.class);

    }

    @Override
    public void deleteById(Long id) {
        SysCompanyContactConfig selectById = companyContactConfigMapper.selectById(id);


        if (selectById.getStatus()==1 || !selectById.getType().equals("dynamic")){
            throw new NcException(ResponseEnum.DELETE_OPERATION_FAIL);

        }else {
            companyContactConfigMapper.deleteById(id);
        }


    }
}
